import settings from "../settings.json";
import languages from "../lang.json";

export const setDateTime = output => {
	const commandValues = output.split("\n");
	const [time, date] = commandValues[0].split("|");
	const username = commandValues[1];
	const [hour, minutes] = time.split(" ");
	const greeting = () => {
		if (hour >= 12 && hour <= 17) {
			return languages[settings.userLang][1]["Good afternoon"];
		} else if (hour > 17 || hour < 5) {
			return languages[settings.userLang][2]["Good evening"];
		} else {
			return languages[settings.userLang][0]["Good morning"];
		}
	};
	const displayDate = new Date(date).toLocaleDateString(settings.userLang, { dateStyle: settings["dateFormat"] });
	return {
		datetime: {
			hour: hour,
			minutes: minutes,
			displayDate: displayDate,
			greeting: greeting(),
			username: username
		}
	};
};

export const setSysInfo = output => {
	const commandValues = output.split("\n");
	const processes = commandValues[0].match(/\d+(\.\d+)?/g);
	const load = commandValues[2].match(/\d+(\.\d+)?/g);
	const cpu = commandValues[3].match(/\d+(\.\d+)?/g);
	const memory = commandValues[6].match(/\d+(\.\d+)?[A-Z]?/g);

	return {
		sysinfo: {
			load: load,
			cpu: cpu,
			processes: processes,
			memory: memory
		}
	};
};

export const setWeather = async () => {
	if (settings.api.city) {
		return fetch(
			`${settings.api.url_base}units=${settings.api.units}&lang=${settings.userLang}&q=${settings.api.city}&appid=${settings.api.key}`
		).then(res => res.json());
	} else {
		return fetch(
			`${settings.api.url_base}units=${settings.api.units}&lang=${settings.userLang}&lat=${settings.api.lat}&lon=${settings.api.long}&appid=${settings.api.key}`
		).then(res => res.json());
	}
};
